package org.javaforever.gatescore.complexverb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontManyToMany;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;

public class ListMyAvailableActive extends FrontTwinsVerb{
	private static final long serialVersionUID = -7160283265794658162L;
	
	public ListMyAvailableActive(FrontManyToMany mtm) {
		super();
		this.master = mtm.getMaster();
		this.slave = mtm.getSlave();
		this.verbName = "ListAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id";
	}
	
	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("listAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id");
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,2,"listAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id("+this.master.getLowerFirstDomainName()+"Id) {"));
		sList.add(new Statement(2000L,3,"listAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id("+this.master.getLowerFirstDomainName()+"Id).then("));
		sList.add(new Statement(3000L,4,"response => {"));
		sList.add(new Statement(4000L,5,"this.availableActive"+this.slave.getAliasOrNamePlural()+" = []"));
		sList.add(new Statement(5000L,5,"for (var i = 0; i < response.data.rows.length; i++) {"));
		sList.add(new Statement(6000L,6,"this.availableActive"+this.slave.getAliasOrNamePlural()+".push(response.data.rows[i]."+this.slave.getDomainId().getLowerFirstFieldName()+")"));
		sList.add(new Statement(7000L,5,"}"));
		sList.add(new Statement(8000L,5,"console.log('JerryDetect:' + this.availableActive"+this.slave.getAliasOrNamePlural()+")"));
		sList.add(new Statement(9000L,4,"})"));
		sList.add(new Statement(10000L,2,"},"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("listAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id");
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"export function listAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id("+this.master.getLowerFirstDomainName()+"Id) {"));
		sList.add(new Statement(2000L,1,"return request({"));
		if (this.master.isUseController()) {
			sList.add(new Statement(3000L,2,"url: '"+this.master.getLowerFirstDomainName()+this.master.getControllerNamingSuffix()+"/listAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id',"));
		} else {
			sList.add(new Statement(3000L,2,"url: '"+this.master.getLowerFirstDomainName()+"Facade/listAvailableActive"+this.master.getCapFirstDomainName()+""+this.slave.getAliasOrNamePlural()+"Using"+this.master.getCapFirstDomainName()+"Id',"));
		}
		sList.add(new Statement(4000L,2,"method: 'post',"));
		if (this.master.isUseOnlyJson() == true) {
			sList.add(new Statement(5000L,2,"data: { "+this.master.getLowerFirstDomainName()+"Id: "+this.master.getLowerFirstDomainName()+"Id }"));
		} else {
			sList.add(new Statement(5000L,2,"data: qs.stringify({ "+this.master.getLowerFirstDomainName()+"Id: "+this.master.getLowerFirstDomainName()+"Id })"));
		}
		sList.add(new Statement(6000L,1,"})"));
		sList.add(new Statement(7000L,0,"}"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;

	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
